package com.oauth.facebook.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;
import com.oauth.facebook.client.beans.FacebookModel;
import com.oauth.facebook.client.beans.LinkedinOAuthVerifier;
import com.oauth.facebook.client.rpc.FacebookRemoteService;
import com.oauth.facebook.client.rpc.FacebookRemoteServiceAsync;

public class MainPanel extends Composite {
	public static final String applicationName = "Facebook";

	private static MainPanelUiBinder uiBinder = GWT.create(MainPanelUiBinder.class);

	interface MainPanelUiBinder extends UiBinder<DecoratedStackPanel, MainPanel> {
	}

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final FacebookRemoteServiceAsync service = GWT.create(FacebookRemoteService.class);

	@UiField
	DecoratedStackPanel stackPanel;

	@UiField
	DeckPanel deckPanel;

	@UiField
	LoginPanel loginPanel;

	@UiField
	HomePanel homePanel;

	public MainPanel() {
		initWidget(uiBinder.createAndBindUi(this));
		loginPanel.setService(service);
		homePanel.setService(service);
		stackPanel.setStackText(0, "<div class=\"stackText\">" + applicationName + "</div>", true);
	}

	public void initMainPanel() {
		deckPanel.showWidget(0);
		String oauthVerifier = Window.Location.getParameter("code");
		LinkedinOAuthVerifier verifier = new LinkedinOAuthVerifier(oauthVerifier);
		service.getLinkedInData(verifier, new OAuthAsnyCallBack<FacebookModel>() {

			public void onSuccess(FacebookModel result) {
				if (result.isLoginView()) {
					deckPanel.showWidget(1);
				} else {
					homePanel.setData(result);
					deckPanel.showWidget(2);
				}
			}

		});
	}

}